<?php require_once "common/header.php";?>
<script type="text/javascript" src="/style/js/sweet-alert.min.js"></script>
<link type="text/css" rel="stylesheet" href="/style/css/sweet-alert.css">
<script src="/style/js/picker.min.js" type="text/javascript" charset="utf-8"></script>
<body>
<div class="outwrap">
    <!--top-->
    <!-- <div class="topbar clearfix topvh">
                <a href="/myshop" class="btn-back">
        <a href="javascript:history.go(-1);" class="btn-back">
            <i class="ico-back"></i><span>返回</span>
        </a>
    </div> -->
    <div class="meinner-adr-add">
        <div class="adr-box-add">
            <div class="iptform">
                <form action="#" id="form_address">
                    <div class="item-ipt">
                        <span class="name">收货人</span><input id="i_realname" type="text" placeholder="请输入姓名" class="txt"/>
                    </div>
                    
                    <div class="item-ipt">
                        <span class="name">手机号码</span><input id="i_mobile" type="tel" placeholder="请输入手机号码" class="txt"/>
                    </div>

                    <div class="item-ipt item-slt">
                        <span class="name">所在地区</span>
                        <div class="content-block">
                            <input id="sel_city" type="text" readonly="" placeholder="请选择">
                            <input id="region_value" type="hidden" value="">
                        </div>
                    </div>
                    <div class="item-ipt" style="display:none;">
                        <span class="name">身份证号码</span><input id="_idnum" type="text" placeholder="购买国内产品可不填写" class="txt"/>
                    </div>
                    <div class="item-ipt">
                        <textarea id="i_address" name="" rows="" cols="" placeholder="详细地址" class="area"></textarea>
                    </div>
                </form>
            </div>

            <div class="item-white clearfix">
                <span class="set-default moren" id="_default"><i class="ico"></i>设置默认地址</span>
            </div>

            <div class="btnwrap">
                <input type="button" class="btn-mexl btn-bc" value="保存" onclick="submitAddress()"/>
            </div>
        </div>
    </div>


</div>
<script type="text/javascript">

$(document).ready(function (e) {

    $("#_province_id").bind("change", function () {

        _get_region($(this).val(), "_city_id");
    });

    $("#_city_id").bind("change", function () {

        _get_region($(this).val(), "_county_id");
    });

});

function _get_region(region_id, obj_id) {
    if (obj_id == "_city_id") {
        $("#_city_id,#_county_id").empty();
        $("#_city_id").prev('.slt').val("请选择");
        $("#_county_id").prev('.slt').val("请选择");
        $("#_city_id").append('<option value="">市/区</option>');
        $("#_county_id").append('<option value="">县/区</option>');
    }
    else if (obj_id == "_county_id") {
        $("#_county_id").prev('.slt').val("请选择");
        $("#_county_id").empty();
        $("#_county_id").append('<option value="">县/区</option>');
    }

    if (region_id != "") {
        $.post("/cart/ajax", {
            "act": "region",
            "parent_id": parseInt(region_id),
            "mr": Math.random()
        }, function (msg) {
            if (msg != "") {
                var json = eval("(" + msg + ")");
                if (json.code == 200) {
                    $.each(json.data, function (region_id, region_name) {

                        $("#" + obj_id).append('<option value="' + region_id + '">' + region_name + '</option>');

                    });
                }
            }
        });
    }
}

function testidnum(val){
  var ids = /^([0-9]{17})+([0-9x]{1})$/;
  val = val.toLowerCase(val);
  if( ids.test(val) ){
    var WI=Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2),RC=Array(1,0,"x",9,8,7,6,5,4,3,2),SUM=0,TMP=0;
    var RCSTR=val.substr(17,1),RCNUM="";
    for(var i=0;i<17;i++){
      TMP = parseInt( val.substr(i,1) );
      SUM += TMP * WI[i];
    }
    RCNUM = RC[ SUM%11 ];
    if(RCNUM==RCSTR) return true;
  }
  return false;
}

function submitAddress() {

    var _do = true;

    if ($("#i_realname").prop("value") == "") {
        swal("请填写收货人姓名");
        _do = false;
    }    

    if (_do) {

        if ($("#i_mobile").prop("value") == "") {
            swal("请填写手机号码");
            _do = false;
        } else {
            pm = /^(13|14|15|16|17|18|19)([0-9]{9})$/;
            if (!pm.test($("#i_mobile").prop("value"))) {
                swal("手机号码格式不正确");
                _do = false;
            }
        }
    }

    if (_do && $("#region_value").val() == "") {
        swal("请选择所在地区");
        _do = false;
    }

    if (_do && $("#i_address").prop("value") == "") {
        swal("请填写收货地址");
        _do = false;
    }

    if(_do &&  $("#_idnum").prop("value") !="" )
      {
        if(!testidnum($("#_idnum").prop("value")))
          {
            swal("身份证号码错误");
            _do = false;
          }
      }
    

    is_default = 0;
    if ($("#_default").hasClass("moren")) is_default = 1;

    if (_do) {
        $.post("/user/ajax_save_address", {
            "act": "save",
            "realname": $("#i_realname").prop("value"),
            "idnum": $("#_idnum").prop("value"),
            "mobile": $("#i_mobile").prop("value"),
            "address": $("#i_address").prop("value"),
            "region": $("#region_value").prop("value"),
            'is_default': is_default,
            "mr": Math.random()
        }, function (msg) {
            if (msg == 200) {
                window.location.href = "addrList";
            }
            else swal("保存失败，请重试");
        });
    }

}
$(function () {
    $('.set-default').on('click', function () {
        $(this).toggleClass('moren');
    });

    $('.item-slt select').change(function () {
        var thisTxt = $(this).find('option:selected').text();
        $(this).prev('.slt').val(thisTxt);
    });
});

</script>
<script>
    /*区域选择*/

    var city = <?=$this->region;?>;

    var nameEl = document.getElementById('sel_city');

    var first = [];
    /* 省，直辖市 */
    var second = [];
    /* 市 */
    var third = [];
    /* 镇 */

    var selectedIndex = [0, 0, 0];
    /* 默认选中的地区 */

    var checked = [0, 0, 0];
    /* 已选选项 */

    function creatList(obj, list) {
        obj.forEach(function (item, index, arr) {
            var temp = new Object();
            temp.text = item.name;
            temp.value = item.id;
            list.push(temp);
        })

    }

    creatList(city, first);

    if (city[selectedIndex[0]].hasOwnProperty('sub')) {
        creatList(city[selectedIndex[0]].sub, second);
    } else {
        second = [{text: '', value: 0}];
    }

    if (city[selectedIndex[0]].sub[selectedIndex[1]].hasOwnProperty('sub')) {
        creatList(city[selectedIndex[0]].sub[selectedIndex[1]].sub, third);
    } else {
        third = [{text: '', value: 0}];
    }

    var picker = new Picker({
        data: [first, second, third],
        selectedIndex: selectedIndex
    });

    picker.on('picker.select', function (selectedVal, selectedIndex) {
        var text1 = first[selectedIndex[0]].text;
        var text2 = second[selectedIndex[1]].text;
        var text3 = third[selectedIndex[2]] ? third[selectedIndex[2]].text : '';

        var value1 = first[selectedIndex[0]].value;
        var value2 = second[selectedIndex[1]].value;
        var value3 = third[selectedIndex[2]] ? third[selectedIndex[2]].value : 0;

        $("#sel_city").prop("value", text1 + ' ' + text2 + ' ' + text3);
        $("#region_value").prop("value", value1 + ',' + value2 + ',' + value3);
    });

    picker.on('picker.change', function (index, selectedIndex) {
        if (index === 0) {
            firstChange();
        } else if (index === 1) {
            secondChange();
        }

        function firstChange() {
            second = [];
            third = [];
            checked[0] = selectedIndex;
            var firstCity = city[selectedIndex];
            if (firstCity.hasOwnProperty('sub')) {
                creatList(firstCity.sub, second);

                var secondCity = city[selectedIndex].sub[0]
                if (secondCity.hasOwnProperty('sub')) {
                    creatList(secondCity.sub, third);
                } else {
                    third = [{text: '', value: 0}];
                    checked[2] = 0;
                }
            } else {
                second = [{text: '', value: 0}];
                third = [{text: '', value: 0}];
                checked[1] = 0;
                checked[2] = 0;
            }

            picker.refillColumn(1, second);
            picker.refillColumn(2, third);
            picker.scrollColumn(1, 0)
            picker.scrollColumn(2, 0)
        }

        function secondChange() {
            third = [];
            checked[1] = selectedIndex;
            var first_index = checked[0];
            if (city[first_index].sub[selectedIndex].hasOwnProperty('sub')) {
                var secondCity = city[first_index].sub[selectedIndex];
                creatList(secondCity.sub, third);
                picker.refillColumn(2, third);
                picker.scrollColumn(2, 0)
            } else {
                third = [{text: '', value: 0}];
                checked[2] = 0;
                picker.refillColumn(2, third);
                picker.scrollColumn(2, 0)
            }
        }

    });

    picker.on('picker.valuechange', function (selectedVal, selectedIndex) {

    });

    nameEl.addEventListener('click', function () {
        picker.show();
    });
</script>
</body>
</html>